// pages/commodity/commoditySup/commoditySup.js
const app = getApp()
import config from '../../../utils/config.js';
import util from '../../../utils/util.js';
import Toast from '../../../vant/toast/toast';
import Dialog from '../../../vant/dialog/dialog';
import {
  comModel
} from '../../../models/com';
const com = new comModel();

import {
  indexModel
} from '../../../models/index';
const index = new indexModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: config,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '',
      //  导航栏 中间的标题
    },
    height: app.globalData.height,
    navHeight: app.globalData.barheight,
    realWindowHeight: app.globalData.realWindowHeight,
    scroll_height:'',

    id:'', // 供应商ID
    type:'',// 父级页面 1 商品供应商 2 清单供应商

    mulist:[
      {id:"全部",categoryName:"全部"}
    ],
    selectMu:'全部',  //当前选择一级菜单标识
    selectMu2:'', //当前选择二级菜单标识

    show:false,
    

    timer:'',
    name:'',

    topNum:0,   // 滚动条距高

    allPage: 0, // 所有页数
    page: 1, // 第几页
    endFlag: false, // 是否到最后
    dataList: [], // 商品列表
    searchWord:'',
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 设置content高度
    var scroll_height = this.data.realWindowHeight-this.data.navHeight-this.data.height-(103 / 750 * wx.getSystemInfoSync().windowWidth);

    var id = options.id;
    var type = options.type;
    var nvabarData = this.data.nvabarData;
    nvabarData.title = (type==1?'供应商商品列表':'食堂可见供应商商品列表');
    this.setData({
      nvabarData:nvabarData,
      scroll_height: scroll_height,
      type:type,
      id:id
    })
    this.getcategoryList();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 一键添加
  onAddAll:util.throttle(function(e) {
    var _this = this
    Dialog.confirm({
      message: '是否确认一键添加所有？',
      overlayStyle: "background: rgba(0,0,0,.3)!important;"
    }).then(() => {
      com.addProductToCheckList({
        supplierId:this.data.id,
        categoryId:this.data.selectMu2?this.data.selectMu2:(this.data.selectMu=="全部"?'':this.data.selectMu)
      },function(res){
        Toast.success('操作成功');
        _this.reData();
        _this.getList();
      })
    }).catch(() => {
      // on cancel
    });
  }),

  // 点击一级菜单
  onTopMu(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      selectMu2:'',
    })
    if(this.data.selectMu==id){
        this.reData();
        this.getList();
        return
    }else{
      this.setData({
        selectMu:id
      })
    }
    this.reData();
    this.getList();
   
  },

  // 点击二级菜单
  onTopTwe(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      selectMu2:id,
    })
    this.reData();
    this.getList();
  },

  // 获取所有分类
  getcategoryList(){
    com.apiCategoryList({}, res => {
      this.setData({
        mulist:this.data.mulist.concat(res)
      })
    })
  },

  // 搜一搜
  searchInput(event){
    clearTimeout(this.data.timer)
    var that = this;
    this.setData({
      timer: setTimeout(function(){
        that.setData({
          topNum:0,
          page:1,
          dataList:[],
          endFlag: false,
          searchWord:event.detail.value
        })
        that.getList();
      },1*1000)
    })
  },

  // 找商品
  getList(){
    if(this.data.type==1){
      index.apiProduct({
        page: this.data.page,
        searchWord:this.data.searchWord,
        supplierId:this.data.id,
        categoryId:this.data.selectMu2?this.data.selectMu2:(this.data.selectMu=="全部"?'':this.data.selectMu),
        limit: 10,
      }, res =>{
        if (res.list.length > 0) {
          var page = this.data.page;
          var endFlag = false;
          var allPage = res.allPage;
          if (page == allPage) {
            endFlag = true;
          } else {
            page += 1;
          }
          var dataList = this.data.dataList;
          if (page != 1) {
            dataList = dataList.concat(res.list);
          } else {
            dataList = res.list;
          }
          this.setData({
            dataList: dataList,
            endFlag: endFlag,
            page: page
          });
        }
      })
    }else{
      com.apiCheckPage({
        page: this.data.page,
        searchWord:this.data.searchWord,
        supplierId:this.data.id,
        categoryId:this.data.selectMu2?this.data.selectMu2:(this.data.selectMu=="全部"?'':this.data.selectMu),
        limit: 10,
      }, res =>{
        if (res.list.length > 0) {
          var page = this.data.page;
          var endFlag = false;
          var allPage = res.allPage;
          if (page == allPage) {
            endFlag = true;
          } else {
            page += 1;
          }
          var dataList = this.data.dataList;
          if (page != 1) {
            dataList = dataList.concat(res.list);
          } else {
            dataList = res.list;
          }
          this.setData({
            dataList: dataList,
            endFlag: endFlag,
            page: page
          });
        }
      })
    }
    
  },

  // 商品滑动至底部
  producScroll(){
    if (this.data.endFlag) {
      util.msg('没有更多了~');
      return;
    } else {
      this.getList();
    }
  },

  // 初始化商品列表
  reData(){
    this.setData({
      topNum:0,
      allPage: 0, // 所有页数
      page: 1, // 第几页
      endFlag: false, // 是否到最后
      dataList: [], // 商品列表
    })
  },

  // 选择规格
  selectMore(e){
    this.setData({
      show:true
    })
    var productId = e.currentTarget.dataset.id;
    var dataList = this.data.dataList;
    for(var i=0;i<dataList.length;i++){
      if(dataList[i].productId == productId){
        this.setData({
          goodListVoList:dataList[i].goodListVoList,
          unit:dataList[i].unitName,
          good:dataList[i].goodListVoList[0]
        })
      }
    }
  },
  // 选中规格
  selectGood(e){
    var goodId = e.currentTarget.dataset.id;
    var goodListVoList = this.data.goodListVoList;
    for(var i=0;i<goodListVoList.length;i++){
      if(goodListVoList[i].goodId == goodId){
        this.setData({
          good:goodListVoList[i]
        })
      }
    }
  },
  // 单个规格加入清单
  selectOne(e){
    var productId = e.currentTarget.dataset.id;
    var dataList = this.data.dataList;
    for(var i=0;i<dataList.length;i++){
      if(dataList[i].productId == productId){
        this.setData({
          good:dataList[i].goodListVoList[0]
        })
      }
    }
    this.getAddcheck();
  },
  // 单个规格移除清单
  removeSelect: util.throttle(function (e){
    var productId = e.currentTarget.dataset.id;
    var dataList = this.data.dataList;
    for(var i=0;i<dataList.length;i++){
      if(dataList[i].productId == productId){
        this.setData({
          good:dataList[i].goodListVoList[0]
        })
      }
    }
    this.getRemoveCheck();
  }),
  onCancel(){
    this.setData({
      show:false
    })
  },

  // 多规格操作
  onPopupMore: util.throttle(function (e) {
    if(this.data.good.checkListStatus==0){ // 加入清单
      this.getAddcheck()
    }else{ // 移除清单
      this.getRemoveCheck();
    }
  }),
  
  // 加入清单
  getAddcheck(){
    Dialog.confirm({
      message: '确定加入清单？',
      overlayStyle: "background: rgba(0,0,0,.3)!important;"
    }).then(() => {
      com.apiAddcheck({goodId:this.data.good.goodId}, res => {
        Toast.success('操作成功');
        this.setData({
          show:false
        })
        this.reData();
        this.getList();
      })
    }).catch(() => {
      // on cancel
    });
    
  },
  // 移除清单
  getRemoveCheck(){
    var _this = this
    Dialog.confirm({
      message: '确定移出清单？',
      overlayStyle: "background: rgba(0,0,0,.3)!important;"
    }).then(() => {
      com.apiDeletecheck({goodId:this.data.good.goodId}, res => {
        Toast.success('操作成功');
        _this.setData({
          show:false
        })
        _this.reData();
        _this.getList();
      })
    }).catch(() => {
      // on cancel
    });
    
  },

  // 清单
  onDetailed: util.throttle(function (e) { 
    wx.navigateTo({
      url: '/pages/coreAdmin/detailedList/detailedList',
    })
  }),

  // 商品详情
  onDetail: util.throttle(function (e) {
    var productId = e.currentTarget.dataset.id
    if (productId) 
      wx.navigateTo({
        url: '/pages/commodity/commodityInfo/commodityInfo?productId=' + productId+'&addt=2',
      })
  }),
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.reData();
    this.data.mulist = [
      {id:"全部",categoryName:"全部"}
    ]
    this.getcategoryList()
    this.getList();
    //模拟加载 		
    setTimeout(function () {		
      wx.hideNavigationBarLoading() //完成停止加载 			
      wx.stopPullDownRefresh() //停止下拉刷新 		
    }, 1500);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.producScroll()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})